package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Banner;
import com.baizhi.springboot.service.BannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @RequestMapping("/selectBannerByPage")
    @ResponseBody
    public Map selectBannerByPage(Integer page, Integer limit) {
        return bannerService.selectBannerByPage(page,limit);
    }
    @RequestMapping("/upload")
    @ResponseBody
    public Map  upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //String realPath = request.getServletContext().getRealPath("/image");
        file.transferTo(new File("D:/uploadFile/"+file.getOriginalFilename()));
        String bannerImageUrl=file.getOriginalFilename();
        System.out.println(bannerImageUrl);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("bannerImageUrl",bannerImageUrl);
        return map;
    }
    @RequestMapping("/insertBanner")
    @ResponseBody
    //@RequiresPermissions("banner:create")
    public Map  insertBanner(Banner banner) throws IOException {
        HashMap<Object, Object> map = new HashMap<>();
        banner.setBannerDate(new Date());
        banner.setBannerState(0);
        try {
            bannerService.insertBanner(banner);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteBanner")
    @ResponseBody
    //@RequiresRoles("admin")
    public Map  deleteBanner(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            bannerService.deleteBanner(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneBanner")
    @ResponseBody
    public Map selectOneBanner(Integer id, HttpSession session) {
       Banner banner= bannerService.selectOneBanner(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("banner", banner);
        return map;
    }

    @RequestMapping("/updateBanner")
    @ResponseBody
    //@RequiresPermissions("banner:update")
    public Map updateBanner(Banner banner) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            bannerService.updateBanner(banner);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

}
